import Vue from 'vue'
import * as Util from '../components/Util'

let MachineChargeService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.MachineChargeService = Vue.prototype.$MachineChargeService = MachineChargeService
  },
  marchineCharge (model, row) {
    let data = {
      f_userinfo_id: row.f_userinfo_id,
      version: row.userinfo_version,
      f_balance: model.f_curbalance,
      overdues: {},
      // garbages: [],
      handplan_ids: []
    }
    let prewater = 0
    let preamount = 0
    let blance = 0
    let last_tablebase = row.rows ? row.rows[0].f_last_tablebase : 0
    let tablebase = 0
    row.rows.forEach((item,index) => {
      prewater = (prewater - 0) + (item.f_oughtamount - 0)
      preamount = (preamount - 0) + (item.f_oughtfee - 0)
      blance = (model.f_collection - 0) - preamount
      tablebase = item.f_tablebase
      data.handplan_ids.push(item.handplan_id)
      if ((model.f_collection - 0) + (row.logicmodel.f_balance - 0) - (preamount - 0) < 0) {
        prewater = (prewater - 0) - (item.f_oughtamount - 0)
        preamount = (preamount - 0) - (item.f_oughtfee - 0)
        blance = (model.f_collection - 0) - preamount
        tablebase = item.f_last_tablebase
        data.handplan_ids.splice(index, 1)
      }
      // let garbage = {
      //   f_userinfo_id: row.f_userinfo_id,
      //   f_price_type: '垃圾费',
      //   f_state: '有效',
      //   f_price: model.f_garbage_price,
      //   f_population: model.f_meter_population,
      //   f_money: model.f_garbage_fee,
      //   f_hand_id: item.handplan_id,
      //   f_hand_date: item.f_hand_date
      // }
      // data.garbages.push(garbage)
    })
    data.overdues = {
      f_user_name: row.f_user_name,
      f_user_id: row.f_user_id,
      userinfo: row.f_userinfo_id,
      f_operat_type: '机表收费',
      f_describe: '对用户' + row.f_user_name + '进行机表收费',
      f_state: '有效',
      f_delaypay: model.f_delaypay,
      f_charge_type: '机表收费',
      // f_garbage_type: row.f_garbage_type,
      f_preamount: preamount,
      f_real_prewater: prewater,
      f_tablebase: tablebase,
      f_last_tablebase: last_tablebase,
      f_balance: row.f_balance,
      f_curbalance: model.f_curbalance,
      f_meter_type: row.f_meter_type,
      f_payment: model.f_payment[0],
      f_print_style: model.f_print[0],
      f_user_type: row.f_user_type,
      f_waterproperties: row.f_waterproperties,
      f_address: row.f_address,
      f_meter_brand: row.f_meter_brand,
      f_meter_style: row.f_meter_style,
      f_meternumber: row.f_meternumber,
      f_coefficient: row.f_coefficient,
      f_meterbook_id: row.f_meterbook_id,
      f_meterbook_number: row.f_meterbook_number,
      f_operator: Util.f.name,
      f_charge_state: '有效',
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname
    }
    if ((model.f_collection - 0) - (model.f_preamount - 0) > 0) {
      data.overdues.f_collection = (model.f_collection - 0) - (blance - 0)
    } else {
      data.overdues.f_collection = (model.f_collection - 0)
    }
    Vue.console.info(data, 'MachineChargeService')
    return Vue.resetpost('rs/logic/machineCharge', {data: data}, {resolveMsg: null, rejectMsg: '机表收费失败'})
  }
}

export default MachineChargeService
